{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 参数实验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/user/miniconda3/envs/yjq-ml/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append('../common/')\n",
    "from evaluator import *\n",
    "from utils import *\n",
    "import warnings\n",
    "import os\n",
    "import numpy as np\n",
    "import torch\n",
    "import sys\n",
    "from usad_model import  *\n",
    "from torch.utils.data import DataLoader\n",
    "from torch.utils.data.sampler import SubsetRandomSampler\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_datase(dataset):\n",
    "    folder = os.path.join(\"../processed\", dataset)\n",
    "    if not os.path.exists(folder):\n",
    "        raise Exception(\"Processed Data not found.\")\n",
    "    loader = []\n",
    "    for file in [\"train\", \"test\", \"labels\"]:\n",
    "        loader.append(np.load(os.path.join(folder, f\"{file}.npy\")))\n",
    "    ## 准备数据\n",
    "    train_data = loader[0]\n",
    "    test_data = loader[1]\n",
    "    labels = loader[2][:,0].reshape(-1,1)\n",
    "    return train_data, test_data, labels\n",
    "def convert_to_windows(data, window_size):\n",
    "    windows = []\n",
    "    length  = data.shape[0]\n",
    "    for i in range(length):\n",
    "        if length - i >= window_size:\n",
    "            window = data[i:i+window_size]\n",
    "        else:\n",
    "            window = data[i-window_size+1:i+1]\n",
    "        windows.append(window)    \n",
    "    return np.array(windows)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 不同z维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "window_size = 12\n",
    "\n",
    "batch_size = 1024\n",
    "num_epochs = 70\n",
    "z_dims = [5,10,20,40,100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data, test_data, labels = load_datase(\"SWaT\")\n",
    "train_window = convert_to_windows(train_data, window_size)\n",
    "test_window = convert_to_windows(test_data, window_size)\n",
    "train_dataset = torch.from_numpy(train_window).float().view([train_window.shape[0], train_window.shape[1]*train_window.shape[2]])\n",
    "test_dataset =  torch.from_numpy(test_window).float().view([test_window.shape[0], test_window.shape[1]*test_window.shape[2]])\n",
    "train_gaussian_percentage  = 0.2\n",
    "indices = np.random.permutation(len(train_dataset))\n",
    "split_point = int(train_gaussian_percentage * len(train_dataset))\n",
    "train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, drop_last=True,\n",
    "                            sampler=SubsetRandomSampler(indices[:-split_point]), pin_memory=False)\n",
    "val_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, drop_last=True,\n",
    "                        sampler=SubsetRandomSampler(indices[-split_point:]),pin_memory=False)\n",
    "test_loader = DataLoader(dataset = test_dataset, batch_size=batch_size, shuffle=False, drop_last=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [18:08<00:00, 15.55s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7649336193530082, 'precision': 0.9934198979372345, 'recall': 0.6219036633122967, 'TP': 33969.0, 'TN': 395073.0, 'FP': 225.0, 'FN': 20652.0, 'threshold': 19.432501098632756}\n",
      "{'f1-score': 0.8429766817028455, 'precision': 0.9028056529305055, 'recall': 0.7905933613828758, 'TP': 43183.0, 'TN': 390649.0, 'FP': 4649.0, 'FN': 11438.0, 'threshold': 9.749492950439448}\n",
      "10\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [18:05<00:00, 15.51s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7649111006343293, 'precision': 0.9933906530404776, 'recall': 0.6218853553355146, 'TP': 33968.0, 'TN': 395072.0, 'FP': 226.0, 'FN': 20653.0, 'threshold': 19.33571940612789}\n",
      "{'f1-score': 0.8395676794308019, 'precision': 0.8950215551121244, 'recall': 0.7905933613828758, 'TP': 43183.0, 'TN': 390233.0, 'FP': 5065.0, 'FN': 11438.0, 'threshold': 9.706457036972045}\n",
      "20\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [16:03<00:00, 13.76s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7649111006343293, 'precision': 0.9933906530404776, 'recall': 0.6218853553355146, 'TP': 33968.0, 'TN': 395072.0, 'FP': 226.0, 'FN': 20653.0, 'threshold': 19.637140335082385}\n",
      "{'f1-score': 0.8466209121294085, 'precision': 0.9112067690255097, 'recall': 0.7905933613828758, 'TP': 43183.0, 'TN': 391090.0, 'FP': 4208.0, 'FN': 11438.0, 'threshold': 10.032476289749024}\n",
      "40\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [15:56<00:00, 13.67s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7649336193530082, 'precision': 0.9934198979372345, 'recall': 0.6219036633122967, 'TP': 33969.0, 'TN': 395073.0, 'FP': 225.0, 'FN': 20652.0, 'threshold': 19.654713897705054}\n",
      "{'f1-score': 0.8429766817028455, 'precision': 0.9028056529305055, 'recall': 0.7905933613828758, 'TP': 43183.0, 'TN': 390649.0, 'FP': 4649.0, 'FN': 11438.0, 'threshold': 9.834795196533193}\n",
      "100\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [16:09<00:00, 13.85s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'f1-score': 0.7649336193530082, 'precision': 0.9934198979372345, 'recall': 0.6219036633122967, 'TP': 33969.0, 'TN': 395073.0, 'FP': 225.0, 'FN': 20652.0, 'threshold': 19.83146098327635}\n",
      "{'f1-score': 0.8429931380061746, 'precision': 0.9028434035327527, 'recall': 0.7905933613828758, 'TP': 43183.0, 'TN': 390651.0, 'FP': 4647.0, 'FN': 11438.0, 'threshold': 9.830964820861665}\n"
     ]
    }
   ],
   "source": [
    "results1 = {}\n",
    "results2 = {}\n",
    "for z_dim in z_dims:\n",
    "    print(z_dim)\n",
    "    # 转换为时间窗口\n",
    "\n",
    "    model = UsadModel(train_data[0].shape[0]*window_size, z_dim)\n",
    "    history = model.fit(\n",
    "        train_loader=train_loader,\n",
    "        validation_loader=val_loader,\n",
    "        epochs=num_epochs\n",
    "    )\n",
    "    pred= model.predict_prob(test_loader)\n",
    "    result1 = bf_search(labels.squeeze(), pred, verbose=False, is_adjust=False) \n",
    "    print(result1)\n",
    "    result2 = bf_search(labels.squeeze(), pred, verbose=False, is_adjust=True)\n",
    "    print(result2)\n",
    "    results1[z_dim] = result1\n",
    "    results2[z_dim] = result2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.7649336193530082, 0.7649111006343293, 0.7649111006343293, 0.7649336193530082, 0.7649336193530082]\n",
      "[0.9934198979372345, 0.9933906530404776, 0.9933906530404776, 0.9934198979372345, 0.9934198979372345]\n",
      "[0.6219036633122967, 0.6218853553355146, 0.6218853553355146, 0.6219036633122967, 0.6219036633122967]\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results1.values())]\n",
    "precisions = [item['precision'] for item in list(results1.values())]\n",
    "recalls = [item['recall'] for item in list(results1.values())]\n",
    "print(f1_scores)\n",
    "print(precisions)\n",
    "print(recalls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.8429766817028455, 0.8395676794308019, 0.8466209121294085, 0.8429766817028455, 0.8429931380061746]\n",
      "[0.9028056529305055, 0.8950215551121244, 0.9112067690255097, 0.9028056529305055, 0.9028434035327527]\n",
      "[0.7905933613828758, 0.7905933613828758, 0.7905933613828758, 0.7905933613828758, 0.7905933613828758]\n"
     ]
    }
   ],
   "source": [
    "f1_scores = [item['f1-score'] for item in list(results2.values())]\n",
    "precisions = [item['precision'] for item in list(results2.values())]\n",
    "recalls = [item['recall'] for item in list(results2.values())]\n",
    "print(f1_scores)\n",
    "print(precisions)\n",
    "print(recalls)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yjq-ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
